<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px" style="height: 600px;overflow: auto;">
			<el-form-item label="岗位名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入岗位名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="岗位编码" prop="code">
				<el-input v-model="form.code" placeholder="请输入岗位编码" clearable></el-input>
			</el-form-item>
			<el-form-item label="所属角色" prop="role_id">
				<el-select v-model="form.role_id" multiple filterable style="width: 100%">
					<el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1" style="width: 30%;"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
            <el-form-item label="提成类型" prop="type">
                <el-radio-group v-model="form.type">
                    <el-radio :label="0" size="large">销售额百分比</el-radio>
                    <el-radio :label="1" size="large">毛利率百分百</el-radio>
                </el-radio-group>
			</el-form-item>
            <el-form-item label="销售额设置" prop="rate" v-if="form.type==0">
				<el-input v-model="rate" clearable></el-input>
			</el-form-item>
            <el-form-item label="毛利率设置" prop="commission_detail" v-if="form.type==1">
                <sc-form-table ref="table" class="formtable" v-model="form.commission_detail" :addTemplate="commission" placeholder="暂无数据" height="200">
                    <el-table-column label="毛利率大于等于" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.big" placeholder="毛利率大于等于" type="number"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="毛利率小于" align="center" prop="money">
                        <template #default="scope">
                            <el-input v-model="scope.row.small" placeholder="毛利率小于" type="number"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="提成比例" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.rate" placeholder="提成比例" type="number"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				code: "",
				name: "",
				sort: 1,
				status: 1,
				remark: "",
				role_id: [],
                type:0,
                commission_detail:[
                    {big:"",small:"",rate:"",}
                ],
			},
            rate:"",
            commission:{big:"",small:"",rate:"",},
			//验证规则
			rules: {
				sort: [
					{ required: true, message: '请输入排序', trigger: 'change' }
				],
				name: [
					{ required: true, message: '请输入岗位名称' }
				],
				code: [
					{ required: true, message: '请输入岗位编码' }
				],
				role_id: [
					{ required: true, message: '请选择所属角色' }
				],
			},
			roles: [],
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		//加载树数据
		async getGroup() {
			var res = await this.$API.system.role.list.get();
			this.roles = res.data.rows;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		//表单提交方法
		submit() {
            if(this.form.type==0){
                this.form.commission_detail[0].big = 0
                this.form.commission_detail[0].small = 0
                this.form.commission_detail[0].rate = this.rate
            }else{
                let array = []
                this.form.commission_detail.map(item=>{
                    array.push({
                        id:Number(item.id),
                        big:Number(item.big),
                        small:Number(item.small),
                        rate:Number(item.rate)
                    })
                })
                this.form.commission_detail = array
            }
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.system.jobs.edit.post(this.form);
					} else {
						res = await this.$API.system.jobs.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.status = data.status
			this.form.sort = data.sort
			this.form.code = data.code
			this.form.remark = data.remark
			this.form.role_id = data.role_id
            this.form.type = data.type
            this.form.commission_detail = data.commission_detail
            this.rate = data.commission_detail[0].rate
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
